/**
 * 
 */
package com.eshopper.user.security;

import java.util.ArrayList;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.eshopper.user.model.User;

/**
 * @author Arun Kumar, Nov 27, 2014
 *
 */
public class SecurityUser implements UserDetails {

	private static final Logger LOGGER = LoggerFactory.getLogger(SecurityUser.class);

	private transient User user;
	private static final long serialVersionUID = 1L;

	public SecurityUser(User user) {
		if (user != null) {
			
			System.out.println("SecurityUser Constructor :" + user);
			this.user = user;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.core.userdetails.UserDetails#getAuthorities
	 * ()
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		
		
		
		SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
		authorities.add(authority);
		
		/*Set<UserRole> userRoles = getUser().getUserRole();
		if (userRoles != null) {
			for (UserRole role : userRoles) {
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(
						role.getRole());
				authorities.add(authority);
			}
		}*/
		
		LOGGER.info("authority :" +authority.toString());
		
		return authorities;
	}

	private User getUser() {
		/*
		 * if ( user == null ) { user = MyUser.findMyUser(userId); if ( user ==
		 * null ) { throw new
		 * RuntimeException("userId in Session refers to no valid User"); } }
		 */
		return user;
	}

	@Override
	public String getPassword() {
		return getUser().getPassword();
	}

	@Override
	public String getUsername() {
		return getUser().getEmail();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
